//
//  HouseInfoGrabViewController.m
//  Jyxt
//
//  Created by 刘日 on 17/5/17.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "HouseInfoGrabViewController.h"
#import "HouseInfoGrabRootDelegate.h"
#import "HouseInfoGrabRootDataSource.h"
#import <YYKit.h>
#import "PhotosTableViewCell.h"
#import "HouseInfoGrabViewModel.h"
#import "FormCommitButtonTableViewCell.h"
#import "HouseInfoGrabViewModel.h"
#import <LemonBubble.h>

@interface HouseInfoGrabViewController ()

@property (nonatomic , strong) HouseInfoGrabRootDelegate *rootDelegate;
@property (nonatomic , strong) HouseInfoGrabRootDataSource *rootDataSource;
@property (nonatomic , strong) NSArray *identifiers;

@end

@implementation HouseInfoGrabViewController

- (void)initBaseInfo{
    self.title = @"出租房源信息采集";
}

- (NSArray *)identifiers{
    if (!_identifiers) {
        _identifiers = @[@"PhotosTableViewCell",@"FormInputTableViewCell",@"FormCommitButtonTableViewCell"];
    }
    return _identifiers;
}

- (HouseInfoGrabRootDelegate *)rootDelegate{
    if (!_rootDelegate) {
        _rootDelegate = [HouseInfoGrabRootDelegate new];
    }
    return _rootDelegate;
}

- (HouseInfoGrabRootDataSource *)rootDataSource{
    if (!_rootDataSource) {
        _rootDataSource = [HouseInfoGrabRootDataSource new];
    }
    return _rootDataSource;
}

- (void)initView{
    self.rootTableView.delegate = self.rootDelegate;
    self.rootTableView.dataSource = self.rootDataSource;
    for (NSString *name in self.identifiers) {
        [self.rootTableView registerNib: [UINib nibWithNibName: name bundle: [NSBundle mainBundle]] forCellReuseIdentifier: name];
    }
    FormCommitButtonTableViewCell *cell = [self.rootTableView dequeueReusableCellWithIdentifier: @"FormCommitButtonTableViewCell"];
    [[cell.button rac_signalForControlEvents: UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIAlertController *alert = [UIAlertController  alertControllerWithTitle: @"提交确认" message: @"您确认要提交信息吗？" preferredStyle: UIAlertControllerStyleAlert];
        [alert addAction: [UIAlertAction actionWithTitle: @"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UITextField *typeField = [[LKInstancePool defaultPool] objectForKey: @"house_type"];
            UITextField *noField = [[LKInstancePool defaultPool] objectForKey: @"house_no"];
            UITextField *addressField = [[LKInstancePool defaultPool] objectForKey: @"house_address"];
            UITextField *areaField = [[LKInstancePool defaultPool] objectForKey: @"house_area"];
            [self showRoundProgressWithTitle: @"提交中.."];
            [HouseInfoGrabViewModel applyHouseInfoWithType: [NSString stringWithFormat: @"%ld" , typeField.tag] no: noField.text address:addressField.text area: areaField.text.floatValue photos: [PhotosTableViewCell photos] owners: [HouseInfoGrabViewModel owners] whenSuccess:^{
                [[PhotosTableViewCell photos] removeAllObjects];
                [[HouseInfoGrabViewModel owners] removeAllObjects];
                [LKFreeCtrl popViewController];
                [self showRightWithTitle: @"提交成功" autoCloseTime: 2];
            } failed:^(NSString *reason) {
                [self showErrorWithTitle: reason autoCloseTime: 3];
            }];
        }]];
        [alert addAction: [UIAlertAction actionWithTitle: @"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController: alert animated: YES completion: nil];
    }];
    self.rootTableView.tableFooterView = cell.contentView;
    
    [[PhotosTableViewCell photos] removeAllObjects];
    [[HouseInfoGrabViewModel owners] removeAllObjects];
}

- (void)viewDidAppear:(BOOL)animated{
    [self.rootTableView reloadSection: 2 withRowAnimation: UITableViewRowAnimationAutomatic];
}

@end
